import java.util.Scanner;
public class Ex6 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int userInput = sc.nextInt();
		long accumulator = 0; // accumulator
		int digitSum = 0;	
		sc.close();
		
		for(int i = 49 ; i < userInput ; i++){		 //the smallest number which it's digits sum is 13 is 49.
													 

			int j = i;
			digitSum = 0;

			while ( j != 0){						// we divide the number by 10 and write up the modolo result as 
													// a digit. We do this , until we can no longer divide by 10.
				digitSum = digitSum + (j % 10);			
				j = j / 10;

			}

			if (digitSum % 13 == 0){				//if the digitSum of j is divided by 13 we accumulate it.
				accumulator = accumulator + i;
			}

		}
		
		System.out.println(accumulator);			

	}
		
		
//		for (int i = 49 ; i < userInput ; i++){
//			digitSum = 0;
//			
//			if(i / 10 != 0){
//				digitSum = i % 10;
//				i = i / 10;
//			}
//		}
		
		
	



}
